package com.webchat.ugc.mongodb.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

public abstract class AbstractMongoService<T> {

    @Autowired
    private MongoTemplate mongoTemplate;

    protected abstract Class<T> getEntityClass();

    /**
     * 校验 collection 是否存在
     *
     * @return
     */
    public boolean collectionExists() {

        return mongoTemplate.collectionExists(getEntityClass());
    }

    /**
     * 创建 collection
     *
     * @return
     */
    public boolean createCollection() {

        if (!collectionExists()) {
            mongoTemplate.createCollection(getEntityClass());
        }
        return true;
    }

    /**
     * 删除集合
     *
     * @return
     */
    public boolean dropCollection() {
        if (!collectionExists()) {
            return true;
        }
        mongoTemplate.dropCollection(getEntityClass());
        return true;
    }
}
